/*
 * Created on 2005-2-8
 *
 */
package bma.common.langutil.core;

import java.lang.reflect.Array;

/**
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class ArrayUtil {
	public static Object add(Object theArray, Object newObject) {
		if (newObject == null)
			return theArray;

		Class cls = theArray == null ? newObject.getClass() : theArray
				.getClass().getComponentType();
		int len = (theArray == null ? 0 : Array.getLength(theArray));

		Object buf = Array.newInstance(cls, len + 1);
		if (theArray != null) {
			System.arraycopy(theArray, 0, buf, 0, len);
		}
		Array.set(buf, len, newObject);
		return buf;
	}

	public static Object addFirst(Object theArray, Object newObject) {
		if (newObject == null)
			return theArray;

		Class cls = theArray == null ? newObject.getClass() : theArray
				.getClass().getComponentType();
		int len = (theArray == null ? 0 : Array.getLength(theArray));

		Object buf = Array.newInstance(cls, len + 1);
		if (theArray != null) {
			System.arraycopy(theArray, 0, buf, 1, len);
		}
		Array.set(buf, 0, newObject);
		return buf;
	}

	public static Object addArray(Object theArray, Object newArray) {
		if (newArray == null)
			return theArray;

		Class cls = (theArray == null ? newArray.getClass() : newArray
				.getClass()).getComponentType();
		int len = (theArray == null ? 0 : Array.getLength(theArray));

		Object buf = Array.newInstance(cls, len + Array.getLength(newArray));
		if (theArray != null) {
			System.arraycopy(theArray, 0, buf, 0, len);
		}
		System.arraycopy(newArray, 0, buf, len, Array.getLength(newArray));
		return buf;
	}

	public static Object removeArray(Object theArray, Object removeArray) {
		int l = theArray == null ? 0 : Array.getLength(theArray);
		if (l == 0)
			return theArray;
		int rl = removeArray == null ? 0 : Array.getLength(removeArray);
		if (rl == 0)
			return theArray;

		int c = 0;
		Object[] buf = new Object[l];

		for (int i = 0; i < l; i++) {
			Object v = Array.get(theArray, i);
			for (int j = 0; j < rl; j++) {
				Object rv = Array.get(removeArray, j);
				if (ObjectUtil.equals(rv, v)) {
					continue;
				} else {
					buf[c++] = v;
					break;
				}
			}
		}

		Object r = Array.newInstance(theArray.getClass().getComponentType(), c);
		if (c > 0) {
			System.arraycopy(buf, 0, r, 0, c);
		}
		return r;
	}

	public static Object remove(Object theArray, int index) {
		if (theArray == null)
			return theArray;

		Object r = Array.newInstance(theArray.getClass().getComponentType(),
				Array.getLength(theArray) - 1);
		if (index > 0) {
			System.arraycopy(theArray, 0, r, 0, index);
		}
		if (Array.getLength(theArray) - index - 1 > 0) {
			System.arraycopy(theArray, index + 1, r, index,
					Array.getLength(theArray) - 1 - index);
		}
		return r;
	}

	public static Object removeObject(Object theArray, Object obj) {
		if (theArray == null)
			return theArray;

		int l = Array.getLength(theArray);
		for (int i = 0; i < l; i++) {
			if (ObjectUtil.equals(obj, Array.get(theArray, i))) {
				Object r = Array.newInstance(theArray.getClass()
						.getComponentType(), l - 1);
				if (i > 0) {
					System.arraycopy(theArray, 0, r, 0, i);
				}
				if (l - i - 1 > 0) {
					System.arraycopy(theArray, i + 1, r, i, l - 1 - i);
				}
				return r;
			}
		}

		return theArray;
	}

	public static Object cast(Object theArray, Class cls) {
		if (theArray == null)
			return null;
		int l = Array.getLength(theArray);
		Object r = Array.newInstance(cls, l);
		if (l > 0) {
			System.arraycopy(theArray, 0, r, 0, l);
		}
		return r;
	}
}